package com.geostar.tbsdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.tencent.smtt.sdk.TbsReaderView;

import java.io.File;

/**
 * 此 Activity 使用 TBS （腾讯浏览服务）查看文件
 * <br/>
 * intent参数: filePath - 文件路径
 *
 * @author hanlyjiang
 */
public class TBSFileViewActivity extends AppCompatActivity implements TbsReaderView.ReaderCallback {

    public static final String FILE_PATH = "filePath";

    private static final String TAG = "TBSFileViewActivity";

    private TbsReaderView mTbsReaderView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String filePath = handleIntent();
        if (TextUtils.isEmpty(filePath) || !new File(filePath).isFile()) {
            Toast.makeText(this, "文件不存在", Toast.LENGTH_SHORT).show();
            finish();
        }

        getSupportActionBar().setTitle("查看文件：" + getFileName(filePath));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mTbsReaderView = new TbsReaderView(this, this);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT
        );
        mTbsReaderView.setLayoutParams(layoutParams);
        setContentView(mTbsReaderView, layoutParams);

        displayFile(filePath);
    }

    private String getFileName(String filePath) {
        return filePath.substring(filePath.lastIndexOf("/") + 1, filePath.indexOf("."));
    }


    private String handleIntent() {
        if (getIntent() != null) {
            return getIntent().getStringExtra(FILE_PATH);
        }
        return null;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onCallBackAction(Integer integer, Object long1, Object long2) {
        Log.d(TAG, "onCallBackAction " + integer + "," + long1 + "," + long2);
    }


    private void displayFile(String fileAbsPath) {
        Bundle bundle = new Bundle();
        bundle.putString("filePath", fileAbsPath);
        bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
        // preOpen 需要文件后缀名 用以判断是否支持
        boolean result = mTbsReaderView.preOpen(parseFormat(fileAbsPath), true);
        if (result) {
            mTbsReaderView.openFile(bundle);
        } else {
            Toast.makeText(this, "文件格式不支持", Toast.LENGTH_SHORT).show();
        }
    }


    private String parseFormat(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTbsReaderView.onStop();

    }

    public static void viewFile(MainActivity mainActivity, String s) {
        Intent intent = new Intent(mainActivity,TBSFileViewActivity.class);
        intent.putExtra("filePath",s);
        mainActivity.startActivity(intent);
    }
}
